package com.cy.pj.sys.service.Impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.common.vo.Node;
import com.cy.pj.sys.dao.MenuDao;
import com.cy.pj.sys.pojo.SysMenu;
import com.cy.pj.sys.service.MenuService;
import com.cy.pj.sys.service.aspect.RequiredCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @RequiredCache
    @Override
    public List<SysMenu> findObjects() {
        List<SysMenu> list = menuDao.findObjects();
//        判断其返回结果是否为空或者长度大小是否为0
        if (list == null || list.size() == 0) {
            throw new ServiceException("没有对应信息");
        }
        return list;
    }

    @Override
    public List<Node> findZtreeMenuNodes() {
        return menuDao.findZtreeMenuNodes();
    }

    @Override
    public int saveObject(SysMenu entity) {
        if (entity == null)
            throw new IllegalArgumentException("对象不能为空");
        if (StringUtils.isEmpty(entity.getName()))
            throw new IllegalArgumentException("菜单名不能为空");
        int rows = menuDao.insertObject(entity);
        return rows;

    }

    @Override
    public int updateObject(SysMenu entity) {
        if (entity == null)
            throw new ServiceException("对象不能为空");
        if (StringUtils.isEmpty(entity.getName()))
            throw new ServiceException("菜单名不能为空");
        int rows = menuDao.updateObject(entity);
        if (rows == 0)
            throw new ServiceException("记录可能不存在");
        return rows;
    }


}
